
package com.scs.application.modules.base.controller;


import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.scs.application.core.excel.handler.ExcelDictHandlerImpl;
import com.scs.application.core.exceptions.ExportException;
import com.scs.application.core.exceptions.ImportException;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.ImportResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.*;
import com.scs.application.modules.base.dto.ExcelManufacturerDTO;
import com.scs.application.modules.base.entity.Manufacturer;
import com.scs.application.modules.base.request.ManufacturerQueryRequest;
import com.scs.application.modules.base.service.ManufacturerService;
import com.scs.application.modules.base.vo.ExcelManufacturerVO;
import lombok.AllArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 厂商 controller
 */
@AllArgsConstructor
@RestController
@SuppressWarnings("all")
@RequestMapping("/base/manufacturers")
public class ManufacturerController extends CrudController<Manufacturer, String, ManufacturerService> implements ImAndExAbleController<ManufacturerQueryRequest> {

    /**
     * 新增厂商信息
     *
     * @param entity
     * @return
     */
    @PostMapping
    public CommonResult add(@RequestBody @Valid Manufacturer entity) {
        entity.setSupplierId(UserUtils.getSupplierId());
        boolean bSave = service.add(entity);
        return bSave ? CommonResult.ok("新增成功", entity) : CommonResult.error("厂商名称或社会信用代码不可重复");
    }

    /**
     * 更新厂商信息
     *
     * @param entity
     * @return
     */
    @Override
    @PutMapping
    public CommonResult update(@RequestBody @Valid Manufacturer entity) {
        entity.setSupplierId(UserUtils.getSupplierId());
        boolean bUpdate = service.update(entity);
        return bUpdate ? CommonResult.ok("保存成功", entity) : CommonResult.error("厂商名称或社会信用代码不可重复");
    }

    @GetMapping("/page")
    public PageResult page(ManufacturerQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(ManufacturerQueryRequest request) {
        List<Manufacturer> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    @PutMapping("/batch_save")
    public CommonResult batchSave(@RequestBody @Valid List<Manufacturer> children) {
        Assert.notEmpty(children, "至少有一条采购订单");
        BeanValidators.validateListWithException(validator, children);

        boolean bSuccess = service.saveOrUpdateBatch(children);
        return bSuccess ? CommonResult.ok("保存成功") : CommonResult.error("保存失败");
    }

    /**
     * 厂商导出数据
     */
    @Override
    public void excelExport(ManufacturerQueryRequest request) {
        String fileName = "厂商信息" + DateUtils.format(DateUtils.now(), "yyyyMMddHHmmss") + ".xlsx";

        ExportParams params = new ExportParams("厂商信息", "厂商信息", ExcelType.XSSF);
        params.setDictHandler(ExcelDictHandlerImpl.getInstance());
        request.setSupplierId(UserUtils.isSupplier() ? UserUtils.getSupplierId() : null);
        List<ExcelManufacturerVO> list = service.listExcel(request);
        List<ExcelManufacturerDTO> dtos = Lists.newArrayListWithCapacity(list.size());
        BeanUtils.copyProperties(list, dtos, ExcelManufacturerDTO.class);

        try {
            setDownloadParam(fileName);
            PoiUtils.exportExcel(response().getOutputStream(), "厂商信息", ExcelManufacturerDTO.class, dtos);
        } catch (IOException e) {
            throw new ExportException("导出异常", e);
        }
    }

    /**
     * 厂商 导出数据模板
     */
    @Override
    public void excelTemplate(ManufacturerQueryRequest request) {
        String fileName = "3厂商导入.xlsx";
        ExportParams params = PoiUtils.getDefaultExportParams("厂商导入模板", "厂商信息", true);
        if (UserUtils.isSupplier()) {
            params.setExclusions(new String[]{"供应商名称"});
        }

        ExcelManufacturerDTO mockData = PoiUtils.mockData(ExcelManufacturerDTO.class);

        try {
            setDownloadParam(fileName);
            PoiUtils.exportExcel(response().getOutputStream(), "厂商信息", ExcelManufacturerDTO.class, Lists.newArrayList(mockData));
        } catch (IOException e) {
            throw new ExportException("导出异常", e);
        }
    }

    /**
     * 厂商导入数据
     */
    @Override
    public CommonResult<ImportResult> excelImport(MultipartFile file) throws ImportException {
        return CommonResult.ok("导入成功", service.excelImport(file));
    }


    /***
     * @param entity
     * @Description:  通用保存、更新
     */
    @PostMapping("/saveOrUpdateCommonCustom")
    public CommonResult saveOrUpdateCommonCustom(@Valid @RequestBody Manufacturer entity) {
        if (entity.isNewRecord()) {
            return add(entity);
        }else{
            return update(entity);
        }
    }

}
